var MyTable={
    searchForm:"searchForm",
    searchUrl:"/wxuser/selectListByXml",
	tableId:"lgTable",
	table:null,
	prefix:"/wxuser/"
};
var zTreeObj;
$(function() {
    var defaultColunms = MyTable.initColumn();
    var table = new BSTable(MyTable.tableId, MyTable.searchUrl,defaultColunms);
	table.setQueryParams(getFormJson(MyTable.searchForm));
	table.setSortName("asc");
	table.setSortOrder("gmt_create");
	MyTable.table = table.init(0);
	loadTreeData();
});
function reLoad() {
	MyTable.table.refresh(getFormJson(MyTable.searchForm));
}
function resetSearch(){
	cleanFormVal(MyTable.searchForm);
	MyTable.table.refresh(null);
}

MyTable.initColumn = function () {
    var columns =  [
		{checkbox : true},
		{field : 'nickname',title : '昵称'},
		{field : 'headImgUrl',title : '头像',align : 'center',
			formatter : function(value, row, index) {
				if (value) {
					return '<img src="'+value+'" style="width:30px;height:30px"></img>';
				} else {
					return '';
				}
			}
		},
		{field : 'city',title : '城市'},
		{field : 'tel', title : '电话'},
		{field : 'email', title : '邮箱'},
		{field : 'userName', title : '真实姓名'},
		{field : 'sex',title : '性别',align : 'center',
			formatter : function(value, row, index) {
				if (value == '1') {
					return '<span class="label label-danger">男</span>';
				} else if (value == '2') {
					return '<span class="label label-primary">女</span>';
				}
			}
		},
		{field : 'subscribe',title : '关注状态',align : 'center',
			formatter : function(value, row, index) {
				if (value == '1') {
					return '<span class="label label-danger">未关注</span>';
				} else if (value == '0') {
					return '<span class="label label-primary">已关注</span>';
				}
			}
		},
		{field : 'isApproval',title : '审核状态',align : 'center',
			formatter : function(value, row, index) {
				if (value == '0') {
					return '<span class="label label-danger">未审核</span>';
				} else if (value == '1') {
					return '<span class="label label-primary">已审核</span>';
				}
			}
		},
		{field : 'subscribeTime', title : '关注时间'},
		];
    return columns;
};


function choose(){
	var ids= checkSelect();
	if(ids){
		window.parent[0].getUser(ids);
		var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
		parent.layer.close(index);
	}
}


var setting = {
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "ps", "N": "s" }
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
				rootPId: "-1"
			}
		} ,
		callback: {  
            onCheck: zTreeOnCheck  
        }, 
};


function loadTreeData() {
	EasyAjax.post({url : "/wxgroup/selectTreeList"},function(r){
		//var res=[];
		//res.push(r);
		//loadTree(res);
		zTreeObj=$.fn.zTree.init($("#tag"), setting, r); 
		zTreeObj.expandAll(true);
	});
}

function zTreeOnCheck(event, treeId, treeNode){
	var select= zTreeObj.getCheckedNodes(true);
	var tagIds=[];
	if(select.length>0){//
		$.each(select,function(i,item){
			if(item.id!=-1){
				tagIds.push(item.id);
			}
		})
	}
	var param={"groupIds":tagIds};
	MyTable.table.refresh(param);
}

function checkSelect(){
	var rows = $('#'+MyTable.tableId).bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要操作的用户");
		return null;
	}else{
		return rows;
	}
}

function loadTree(r){
	layui.use(['tree'], function(){
		   var layer = layui.layer,form = layui.form;
		   layui.tree({
			    elem: '#tag' //传入元素选择器
			   ,nodes:r
			   ,click: function(node){
				    if(node.id!=-1){
				    	var param={"groupId":node.id};
						MyTable.table.refresh(param);
				    }
				}  
			 });
	});
}
